class BloggerApi < ActionWebService::API::Base
  inflect_names false

  api_method :deletePost,
    :expects => [ {:appkey => :string}, {:postid => :int}, {:username => :string}, {:password => :string},
                  {:publish => :bool} ],
    :returns => [:bool]

end

class BloggerService < ActionWebService::Base
  web_service_api BloggerApi
  before_invocation :authenticate

  def initialize(controller)
    @controller = controller
  end
  
  def authenticate(name, args)
    method = self.class.web_service_api.api_methods[name]
    begin
      h = method.expects_to_hash(args)
      raise "Invalid login" unless @user=User.authenticate(:login => h[:username], :password => h[:password])
    rescue NoMethodError
      username, password = method[:expects].index(:username=>String), method[:expects].index(:password=>String)
      raise "Invalid login" unless @user=User.authenticate(:login => h[:username], :password => h[:password])
    end
  end

  def deletePost(appkey, postid, username, password, publish)
    post = Post.find(postid)
    post.destroy
    true
  end
  
end
